import OSS from 'ali-oss'

/**
 * 获取配置
 * @param region shanghai beijing
 * @returns {{region: string, accessKeyId: string, accessKeySecret: string, bucket: string}}
 */
function getOption(region = "shanghai") {
  switch (region) {
    case 'shanghai':
      return {
        region: 'oss-cn-shanghai',
        accessKeyId: 'LTAI5tMevYwnihXXHmuSvNiZ',
        accessKeySecret: 'P9mYHb9ddkTZuPPW8DP7LnaIiF3VZS',
        bucket: 'daocuiaidev1'
      }
    case "beijing":
      return {
        region: 'oss-cn-beijing',
        accessKeyId: 'LTAI5tMsSwtA9J1sLs1hqury',
        accessKeySecret: 'pjI2qtN5EccNey9sv5rajxkdLhIvuU',
        bucket: 'health-test-yz'
      }
    default:
      return {
        region: 'oss-cn-shanghai',
        accessKeyId: 'LTAI5tMevYwnihXXHmuSvNiZ',
        accessKeySecret: 'P9mYHb9ddkTZuPPW8DP7LnaIiF3VZS',
        bucket: 'daocuiaidev1'
      }
  }
}

/**
 *  获取Oss
 * @returns {oss}
 */
export function createClient() {
  const ossOptions = getOption('beijing')
  return new OSS({
    // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    accessKeyId: ossOptions.accessKeyId,
    accessKeySecret: ossOptions.accessKeySecret, // yourRegion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
    region: ossOptions.region,
    authorizationV4: true, // yourBucketName填写Bucket名称。
    bucket: ossOptions.bucket
  })
}

export function getSrc(client, src) {
  client = client || createClient()
  if (!src) return ''
  return client.signatureUrl(src, { expires: 999999999999999 }) // 可访问的链接
}
